package dynamic

import (
	"github.com/cht9862/mango/myloger"
	"github.com/gin-gonic/gin"
	"net/http"
	"problemandanswer/models"
)
var logFmt = myloger.NewLog()

// Register 注册用户
func RegisterUser(c *gin.Context){
	models.User.Name = c.PostForm("username")
	models.User.Passwd = c.PostForm("password")
	models.User.Phone = c.PostForm("phone")
	if len(models.User.Phone) != 11 {
		c.JSON(http.StatusBadRequest,gin.H{
			"requestStatus":http.StatusBadRequest,
			"describe": "电话号码不合规",
		})
		return
	}
	err := models.Model.AddUser(models.User)
	if err != nil {
		logFmt.Info("-- > 添加用户失败")
		c.JSON(http.StatusOK,gin.H{
                	"requestStatus":http.StatusOK,
                	"describe": "添加失败",
       		 })
		return
	} else{
		c.JSON(http.StatusOK,gin.H{
			"requestStatus":http.StatusOK,
			"describe": "注册成功",
		})
	}
}

func UpdateUser(c *gin.Context){
	models.User.Name = c.PostForm("username")
	models.User.Passwd = c.PostForm("password")
	models.User.Phone = c.PostForm("phone")
	Oldname := c.PostForm("oldname")
	err := models.Model.SetUser(models.User,Oldname)
	if err != nil {
		logFmt.Info("-- > 修改用户失败")
	}
	c.JSON(http.StatusOK,gin.H{
		"requestStatus":http.StatusOK,
		"describe": "修改成功",
	})
}

func GetAllProblems(c *gin.Context){
	Problems, err := models.Model.GetAllProblems()
	if err != nil {
		c.JSON(http.StatusBadGateway,gin.H{
			"msg":err,
		})
	} else {
		for _, problem := range Problems{
			c.JSON(http.StatusOK,gin.H{
				"Questions":problem.Questions,
				"Answer":problem.Answers,
				"CreateTime":problem.CreateTime,
				"AnswerTime":problem.AnswerTime,
			})
		}
	}
}